﻿using System.IO;
using System.Windows.Forms;
using System.Xml.Serialization;

namespace ChromiumUpdater
{
    public class MySettings
    {
        public int CurrentBuild { get; set; }
        public bool StartAfterUpdating { get; set; }
        public int CheckInterval { get; set; }
        public string Switches { get; set; }

        private const string SettingFile = "Config.ini";

        public static MySettings Load()
        {
            var my = new MySettings();
            var serializer = new XmlSerializer(typeof(MySettings));
            if (CheckSettingExist())
            {
                using (var reader = new StreamReader(SettingFile))
                {
                    var obj = serializer.Deserialize(reader) as MySettings;
                    if (obj != null)
                    {
                        my.StartAfterUpdating = obj.StartAfterUpdating;
                        my.CurrentBuild = obj.CurrentBuild;
                        my.CheckInterval = obj.CheckInterval;
                        my.Switches = obj.Switches;
                    }
                }
                return my;
            }
            my = new MySettings
                     {
                         StartAfterUpdating = false,
                         CheckInterval = 60,
                         CurrentBuild = 0,
                         Switches = null
                     };
            Save(my);
            return my;
        }

        public static void Save(MySettings my)
        {
            var serializer = new XmlSerializer(typeof(MySettings));
            var folder = Application.StartupPath;
            var dest = Path.Combine(folder, SettingFile);
            using (var writer = new StreamWriter(dest))
            {
                serializer.Serialize(writer, my);
            }
        }

        public static bool CheckSettingExist()
        {
            if (File.Exists(SettingFile))
            {
                return true;
            }
            return false;
        }
    }
}
